package com.sishu.service.impl;

import com.sishu.mapper.*;
import com.sishu.pojo.Game;
import com.sishu.pojo.Gamegroups;
import com.sishu.pojo.Locationlibrary;
import com.sishu.pojo.Student;
import com.sishu.service.StudentService;
import com.sishu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StudentServiceimpl implements StudentService {


    @Resource
    private StudentMapper studentMapper;

    @Resource
    private GameMapper gameMapper;

    @Resource
    private GamegroupsMapper gamegroupsMapper;

    @Resource
    private LocationrecordMapper locationrecordMapper;

    @Resource
    private LocationlibraryMapper locationlibraryMapper;

    @Override
    public Result selectsname(String sId) {

        //根据sid查询学生姓名
        Student student = studentMapper.selectbysId(sId);

        if (student != null) {
            return new Result<>(200, "data", "查询成功", student.getSName(), true);
        } else {
            return new Result<>(202, "查询失败", false);
        }
    }


    @Override
    public Result getground(String groupId, String sId, String locationType) {

        //0.差一个检测groupid和sid是不是在数据库中没有做

        if (groupId == null) {
            List<Locationlibrary> selectallground = locationlibraryMapper.selectallground();
            return new Result<>(200, "list", selectallground, "查询成功", true);
        }

        //1.查询groupid所属的gamegroups的active是否为0，为0就是小组游戏结束
        Gamegroups gamegroups = gamegroupsMapper.selectbygroupId(groupId);
        if (gamegroups.getActive() == 0) {
            return new Result<>(202, "该小组游戏已结束", false);
        }

        //2.查询gameid所属的game的active是否为0，为0就是整局游戏结束
        Game selectgame = gameMapper.selectgame(gamegroups.getGameId());
        if (selectgame.getActive() == 0) {
            return new Result<>(202, "本局游戏已结束", false);
        }

        //如果传过来的sid为空的话返回组的地盘表信息
        if (sId == null || sId.isEmpty() || sId.equals("null")) {

            if (locationType == null || ("null").equals(locationType) || locationType.isEmpty()) {
                //如果传过来的地盘种类为空,返回地盘全部信息 (既没有sid也没有sitetype)
                List<Locationlibrary> selectallground = locationlibraryMapper.selectallground();
                return new Result<>(200, "list", selectallground, "查询成功", true);
            }
            //地盘种类不为空，返回该地盘的全部信息
            List<Locationlibrary> allground = locationlibraryMapper.selectbylocationtype(locationType);


            //2.获取当前组已经购买的地盘
            List<Locationlibrary> Purchasedground = locationlibraryMapper.selectPurchasedground1(groupId, locationType);
            System.out.println("Purchasedground = " + Purchasedground);
            //3.获取当前可以购买的地盘
            List<Locationlibrary> availableGround = allground.stream()
                    .filter(mapSite -> !Purchasedground.contains(mapSite))
                    .toList();

            List<List<Locationlibrary>> list = new ArrayList<>();
            list.add(allground);//所有的
            list.add(Purchasedground);//已购买的
            list.add(availableGround);//没有买,还可以买的
            return new Result<>(200, "list", list, "查询成功", true);
        }

        //如果传过来的地盘种类为空,返回该sid的组地盘信息
        if (locationType == null || ("null").equals(locationType) || locationType.isEmpty()) {
            List<Locationlibrary> locationsId = locationlibraryMapper.selectPurchasedground3(sId, groupId);
            return new Result<>(200, "list", locationsId, "查询成功", true);
        }
        //如果都有的话,返回该sid的组对应种类的地盘信息
        List<Locationlibrary> locationlibraries = locationlibraryMapper.selectPurchasedground2(sId, groupId, locationType);

        if (locationlibraries == null || locationlibraries.isEmpty()) {
            return new Result<>(202, "没有购买地盘", true);
        }
        return new Result<>(200, "list", locationlibraries, "查询成功", true);
    }
}
